package com.luyun.frogpay.view.login;

import android.os.Bundle;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.luyun.frogpay.BuildConfig;
import com.luyun.frogpay.R;
import com.luyun.frogpay.base.baseclass.ActivityManager;
import com.luyun.frogpay.base.baseclass.BaseActivity;
import com.luyun.frogpay.base.routepath.RouteActivityPath;
import com.luyun.frogpay.base.utils.EditUtil;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = RouteActivityPath.LOGIN_ACTIVITY)
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView {
    @BindView(R.id.edit_account)
    EditText editAccount;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.cb_psw_visibility)
    CheckBox cbPswVisibility;
    @BindView(R.id.forget)
    TextView forget;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void initView() {
        ActivityManager.getInstance().addActivity(this);
        cbPswVisibilityAddListener();
        EditText[] editTexts = {editAccount, editPassword};
        EditUtil.setBtnEnable(editTexts, btnLogin);
        if (BuildConfig.DEBUG){
            editAccount.setText("00118931105703");
            editPassword.setText("123456");
        }
    }

    @Override
    protected void initInjector() {
        activityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @OnClick(R.id.btn_login)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_login:
                presenter.login();
                break;
        }
    }

    private void cbPswVisibilityAddListener() {
        cbPswVisibility.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    editPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
            }
        });
    }

    @Override
    public String getAccount() {
        return editAccount.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return editPassword.getText().toString().trim();
    }

    @Override
    public void loginSuccess() {
        ARouter.getInstance().build(RouteActivityPath.HOME_ACTIVITY).navigation();
        finish();
    }
}
